package com.miniapp.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * 全局日期格式化配置类
 * 统一处理日期类型的JSON序列化和请求参数反序列化
 *
 * @author yourName
 * @date 2025-09-03
 */
@Configuration
public class DateFormatterConfig {

    /**
     * 日期格式常量
     */
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String TIME_FORMAT = "HH:mm:ss";
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 配置Jackson全局日期序列化格式
     * 处理LocalDateTime、LocalDate、LocalTime类型的JSON输出
     *
     * @return 自定义的Jackson配置
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> {
            // 配置LocalDateTime序列化格式
            builder.serializerByType(LocalDateTime.class,
                    new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));
            // 配置LocalDate序列化格式
            builder.serializerByType(LocalDate.class,
                    new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_FORMAT)));
            // 配置LocalTime序列化格式
            builder.serializerByType(LocalTime.class,
                    new LocalTimeSerializer(DateTimeFormatter.ofPattern(TIME_FORMAT)));

            // 配置反序列化
            builder.deserializerByType(LocalDateTime.class,
                    new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));
            builder.deserializerByType(LocalDate.class,
                    new LocalDateDeserializer(DateTimeFormatter.ofPattern(DATE_FORMAT)));
            builder.deserializerByType(LocalTime.class,
                    new LocalTimeDeserializer(DateTimeFormatter.ofPattern(TIME_FORMAT)));
        };
    }

    /**
     * 配置字符串转LocalDate的转换器
     * 处理请求参数中字符串到LocalDate的转换
     *
     * @return 字符串到LocalDate的转换器
     */
    @Bean
    public Converter<String, LocalDate> localDateConverter() {
        return new StringToLocalDateConverter();
    }

    /**
     * 配置字符串转LocalTime的转换器
     * 处理请求参数中字符串到LocalTime的转换
     *
     * @return 字符串到LocalTime的转换器
     */
    @Bean
    public Converter<String, LocalTime> localTimeConverter() {
        return new StringToLocalTimeConverter();
    }

    /**
     * 配置字符串转LocalDateTime的转换器
     * 处理请求参数中字符串到LocalDateTime的转换
     *
     * @return 字符串到LocalDateTime的转换器
     */
    @Bean
    public Converter<String, LocalDateTime> localDateTimeConverter() {
        return new StringToLocalDateTimeConverter();
    }

    /**
     * 获取自定义的ObjectMapper
     * 用于需要手动序列化对象的场景
     *
     * @return 配置好日期格式的ObjectMapper
     */
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        JavaTimeModule module = new JavaTimeModule();

        // 注册自定义序列化器
        module.addSerializer(LocalDateTime.class,
                new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));
        module.addSerializer(LocalDate.class,
                new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_FORMAT)));
        module.addSerializer(LocalTime.class,
                new LocalTimeSerializer(DateTimeFormatter.ofPattern(TIME_FORMAT)));
        module.addDeserializer(LocalDateTime.class,
                new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));
        module.addDeserializer(LocalDate.class,
                new LocalDateDeserializer(DateTimeFormatter.ofPattern(DATE_FORMAT)));
        module.addDeserializer(LocalTime.class,
                new LocalTimeDeserializer(DateTimeFormatter.ofPattern(TIME_FORMAT)));
        objectMapper.registerModule(module);
        return objectMapper;
    }

    /**
     * 字符串转LocalDate的转换器（显式泛型定义）
     */
    private static class StringToLocalDateConverter implements Converter<String, LocalDate> {
        @Override
        public LocalDate convert(String source) {
            if (source == null || source.trim().isEmpty()) {
                return null;
            }
            return LocalDate.parse(source, DateTimeFormatter.ofPattern(DATE_FORMAT));
        }
    }

    /**
     * 字符串转LocalTime的转换器（显式泛型定义）
     */
    private static class StringToLocalTimeConverter implements Converter<String, LocalTime> {
        @Override
        public LocalTime convert(String source) {
            if (source == null || source.trim().isEmpty()) {
                return null;
            }
            return LocalTime.parse(source, DateTimeFormatter.ofPattern(TIME_FORMAT));
        }
    }

    /**
     * 字符串转LocalDateTime的转换器（显式泛型定义）
     */
    private static class StringToLocalDateTimeConverter implements Converter<String, LocalDateTime> {
        @Override
        public LocalDateTime convert(String source) {
            if (source == null || source.trim().isEmpty()) {
                return null;
            }
            return LocalDateTime.parse(source, DateTimeFormatter.ofPattern(DATE_TIME_FORMAT));
        }
    }
}

